import dayjs from 'dayjs';

// components/date-range/date-range.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    format: { // 属性名
      type: String,
      value: ''
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    showDateSelct: false,
    startDate: '',
    startDateText: '',
    endDate: '',
    endDateText: '',
    minDate: dayjs().add(-3, 'month').valueOf(),
    maxDate: dayjs().add(1, 'month').valueOf(),
  },

  /**
   * 组件的方法列表
   */
  methods: {
    show() {
      this.setData({
        showDateSelct: true
      })
    },
    onDateClose() {
      this.setData({
        showDateSelct: false
      })
    },
    onDateConfirm(event) {
      const [start, end] = event.detail;
      const { format } = this.properties
      this.setData({
        startDate: dayjs(start).format('YYYY-MM-DD HH:mm:ss'),
        endDate: dayjs(end).format('YYYY-MM-DD HH:mm:ss'),
        startDateText: dayjs(start).format(format || 'YYYY-MM-DD HH:mm:ss'),
        endDateText: dayjs(end).format(format || 'YYYY-MM-DD HH:mm:ss')
      })
      this.onDateClose()
      const detail = {
        start: dayjs(start).format('YYYY-MM-DD HH:mm:ss'),
        end: dayjs(end).format('YYYY-MM-DD HH:mm:ss'),
      }
      this.triggerEvent('confirm', detail)
    },
    setDefaultDate([start, end]) {
      const { format } = this.properties
      this.setData({
        startDate: start,
        endDate: end,
        startDateText: dayjs(start).format(format || 'YYYY-MM-DD HH:mm:ss'),
        endDateText: dayjs(end).format(format || 'YYYY-MM-DD HH:mm:ss')
      })
    }
  }
})
